from tornado.ioloop import IOLoop
from tornado.options import options, define

from utils.application import Application

from utils.futurestarserver import FuturestarTCPServer
from utils.logger import get_logger

from urls import urls

logger = get_logger(__name__)

define('port', default=9000, help="TCP port to use", type=int)
define('server', default=False, help="Run as the echo server")
define("encoding", default='utf-8', help="String encoding")


def main():
    options.parse_command_line()

    app = Application(urls)
    server = FuturestarTCPServer(app, ssl_options=None)

    try:
        # 单进程模式
        server.listen(options.port)
        logger.info("futurestar server listen to port: {}".format(options.port))
        IOLoop.current().start()
    except KeyboardInterrupt as e:
        logger.warning("futurestar server is down.")


if __name__ == "__main__":
    main()
